<?php
class excel
    {
    function createExcelDocument($filename, $excelData)
        {


        //$sql, $fileName
     //   $filename="exceltext3.xls";
        // $data = array();

        //  $data[0][0] = "name";
        //  $data[0][1] = "vorname";


        // Fehlermeldungen ausschalten
        error_reporting (E_ERROR);

        //global $rootDir;
        //global $webDir;
        set_include_path (config::$basePath . "/api/PEAR/");

        require_once 'Spreadsheet/Excel/Writer.php';
        //require_once 'Writer.php';


        // Creating a workbook
        $workbook=new Spreadsheet_Excel_Writer();

        $tmpPath =config::$basePath . '/tmp';
        //createFolder($tmpPath);
        $workbook->setTempDir($tmpPath);

        // sending HTTP headers
        //$workbook->send($fileName . '.xls');
        
        //echo $filename;
        $workbook->send($filename);

        //echo "filename ".$filename;

        // Creating a worksheet
        //$worksheet=&$workbook->addWorksheet($fileName);
        $worksheet=&$workbook->addWorksheet("test1");

        //  $worksheet->write(1, 1, "asdfasdfasdf");       
        //  $worksheet->write(2, 2, "ssssssasdfasdfasdf");       

        for ($n=0; $n < sizeof($excelData); $n++)
            {
            for ($m=0; $m < sizeof($excelData[$n]); $m++)
                {

                //$worksheet->write($m, $n, $excelData[$n][$m]);       
                $worksheet->write($n, $m, $excelData[$n][$m]);

                //if ($excelData[$n][$m] == "x")

                }
            }


        /*   $result   =mysql_query($sql);
       
           if (!$result)
               $errorMessage=$errorMessage . mysql_error() . $sql;
       
           $dbField=array();
       
           $dbTable=array();
       
           $i9=0;
       
           while ($i9 < mysql_num_fields($result))
               {
               //    echo "Information für Feld $i:<br />\n";
               $meta     = mysql_fetch_field($result, $i9);
               $dbField[]=strtolower($meta->name);
               $dbTable[]=strtolower($meta->table);
       
               //    $buffer = $buffer." Field: ".$meta->table.strtolower($meta->name)."<br>";
       
               $i9++;
               }
       
           for ($m=0; $m < sizeof($dbField); $m++)
               {
               // Inhalt ersetzen (falls Tabelle angegeben wurde)
               //                $inhalt=str_ireplace("[".$dbTable[$m].".".$dbField[$m]."]", "", $inhalt);
               $worksheet->write(0, $m, $dbField[$m]);
               }
       
           $zaheler=0;
       
           while ($row=mysql_fetch_assoc($result))
               {
               $row = array_change_key_case($row);
       
               $zaheler++;
       
               for ($m=0; $m < sizeof($dbField); $m++)
                   {
                   // Inhalt ersetzen (falls Tabelle angegeben wurde)
                   //                $inhalt=str_ireplace("[".$dbTable[$m].".".$dbField[$m]."]", "", $inhalt);
                   $worksheet->write($zaheler, $m, $row[$dbField[$m]]);
                   }
               }  */


        // Let's send the file
        $workbook->close();
        
        return "tmp/$excelFilename";
        
        }
    
    
     function exportExcel($excelFilename, $sql = "", $table = "", $where = "", $orderby = "", $dbField = "") {
         
         
         //$dataExcel = new data();
         
         //$dataExcel->getTable($table, $where, $orderby);
         
         
         
         
         $n = 0;
         for ($m=0; $m<sizeof($dbField); $m++) {
                 
                 $data[$n][$m] = $dbField[$m];
                 
             }
         $n++;
         
         
         //do {
         foreach (db::getData($table, $where, $orderby) as $dataExcel) {       
             
             for ($m=0; $m<sizeof($dbField); $m++) {
                 
                 $data[$n][$m] = $dataExcel[strtolower($dbField[$m])];
                 
             }
             
             $n++;
            
         } 
             
             
         
        // echo $excelFilename;
         
         $excelFilename = $this->createExcelDocument($excelFilename, $data);
         

         
         return $excelFilename;
         
         
     }
        
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    }
?>